/*
 * Copyright 2015 MingxingWang

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 *     http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.audaque.graphdb

import org.apache.spark.graphx._

class Vertex(val vid: VertexId,
             var prop: Property = null)
  extends Serializable {

  def apply(index: Int): Any = if (prop == null) null else prop(index)

  def apply(name: String): Any = if (prop == null) null else prop(name)

  def getProperty(index: Int): Any = apply(index)

  def getProperty(name: String): Any = apply(name)

  def canEqual(other: Any): Boolean = other.isInstanceOf[Vertex]

  override def equals(other: Any): Boolean = other match {
    case that: Vertex =>
      (that canEqual this) &&
        vid == that.vid
    case _ => false
  }


  def debug() = {
    print("id:" + vid+", ")
    if (prop != null) {
      prop.debug()
    }
    println()
  }

  override def hashCode(): Int = {
    val state = Seq(vid, prop)
    state.map(_.hashCode()).foldLeft(0)((a, b) => 31 * a + b)
  }
}

object Vertex {
  def apply(vid: VertexId): Vertex = new Vertex(vid)

  def apply(vid: VertexId, prop: Property): Vertex = new Vertex(vid, prop)

}
